package com.hjq.fcloud.ui.activity

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.hjq.fcloud.R
import com.hjq.fcloud.bean.FileItemBean
import com.hjq.fcloud.bean.PlatformServiceFeeSignDetailData
import com.hjq.fcloud.bean.FlowDataBean
import com.hjq.fcloud.config.FlowStatusConfig
import com.hjq.fcloud.event.EventBean
import com.hjq.fcloud.event.EventBusUtils
import com.hjq.fcloud.http.api.PlatformServiceFeeDetailApi
import com.hjq.fcloud.http.api.PlatformServiceFeeSignApi
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.ktx.ViewKtx.setVisibility
import com.hjq.fcloud.jump.CommonJumpBean
import com.hjq.fcloud.widget.BottomSignViewClickListener
import com.hjq.http.EasyHttp
import com.hjq.http.body.JsonBody
import com.hjq.http.listener.HttpCallback
import kotlinx.android.synthetic.main.activity_platform_service_fee_sign.*
import kotlinx.android.synthetic.main.layout_sign_result.signResultIv
import okhttp3.Call
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 平台服务费协议网签
 */
class PlatformServiceFeeSignActivity : BaseDetailActivity() {

    companion object {
        //1通过0不通过
        const val STATUS_PASS = "1"
        const val STATUS_NOT_PASS = "0"

        fun start(ac: Context, jumpBean: CommonJumpBean, isDisableAgainSign: Boolean = false) {
            var intent = Intent(ac, PlatformServiceFeeSignActivity::class.java)
            intent.putExtra(INTENT_IN_ITEM, jumpBean)
            intent.putExtra(INTENT_IN_DISABLE_AGAIN_SIGN, isDisableAgainSign)
            ac.startActivity(intent)
        }
    }

    private var mDetail: PlatformServiceFeeSignDetailData? = null
    private var status: String? = STATUS_PASS

    override fun userEventBus(): Boolean {
        return true
    }

    override fun getStateLayout(): View? {
        return statusLayout
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_platform_service_fee_sign
    }

    override fun initView() {

    }

    override fun initData() {
        getServerFlow()

        getServerFlowStatus({ flowStatusBean ->
            if(flowStatusBean.isFlowUnfinished){
                showFlowUnfinishedView(flowStatusBean.flowUnfinishedTipsArray)
                return@getServerFlowStatus
            }

            getServerDetail()

            if (flowStatusBean.canSign || flowStatusBean.canAgainSign) {
                var isAgainSign = flowStatusBean.canAgainSign && !isDisableAgainSign//重新签约
                if (isAgainSign) {//需要展示流程和签约方式
                    flowView.setVisibility(true)
                    //签约方式
                    signModeSb.setVisibility(true)
                } else {
                    //审核控件设置
                    feedbackRootView.setVisibility(true)
                }
                //底部签约控件设置
                bottomView.getRejectSignButton().setVisibility(false)
                bottomView.setViewData(isAgainSign, object :
                    BottomSignViewClickListener {
                    override fun onAgainPassSign() {
                        requestServerResetFlow() {
                            start(context, commonJumpBean!!, true)
                            finish()
                        }
                    }

                    override fun onPassSign() {
                        status = STATUS_PASS
                        if (checkInput()) {
                            FaceDetectRGBActivity.launcher(context, intent.extras)
                        }
                    }

                    override fun onNoPassSign() {
                        status = STATUS_NOT_PASS
                        if (checkInput()) {
                            FaceDetectRGBActivity.launcher(context, intent.extras)
                        }
                    }

                })
            } else {
                flowView.setVisibility(true)
                //签约方式
                signModeSb.setVisibility(true)
            }
        })
    }

    private fun getServerDetail() {
        var api = PlatformServiceFeeDetailApi()
            .setId(commonJumpBean?.bizId)
        EasyHttp.post(this)
            .api(api)
            .request(object :
                HttpCallback<HttpData<PlatformServiceFeeSignDetailData?>?>(this) {
                override fun onStart(call: Call?) {}
                override fun onSucceed(data: HttpData<PlatformServiceFeeSignDetailData?>?) {
                    mDetail = data?.getData()
                    setViewData(mDetail)
                }
            })
    }

    override fun setFlowViewData(data: FlowDataBean?) {
        flowView.setViewData(data?.setup_list)
    }

    private fun setViewData(item: PlatformServiceFeeSignDetailData?) {
        showAppContent()

        if(!isDisableAgainSign){
            signResultIv.setViewData(flowStatusData)
        }
        noSb.setRightText(item?.code)
        projectNameSb.setRightText(item?.project?.project_name)
        sectNameSb.setRightText(item?.sect?.sect_name)
        loanSb.setRightText(item?.loanName)

        serviceFeeRatioSb.setRightText(item?.service_fee_ratio)
        moneySb.setRightText(item?.money)
        signModeSb.setRightText(item?.debtConfirmModeName)

        fileView.setViewData(this, FlowStatusConfig.getFileTitleName(flowStatusData), getAllFiles(item?.files))
    }

    /**
     * 获取文件列表
     */
    fun getAllFiles(files: LinkedHashMap<String, ArrayList<FileItemBean>>?): ArrayList<FileItemBean>? {
        var file571 = files?.get("fnc-57-1")
        var file570 = files?.get("fnc-57-0")
        //先判断有57-1显示 57-1 没有显示 57-0
        if(file571 != null && file571.size > 0){
            return file571
        }
        return file570
    }


    private fun checkInput(): Boolean {
        if (TextUtils.isEmpty(feedbackView.getInPutText())) {
            showToast("请输入意见反馈")
            return false
        }
        return true
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMainEventBus(bean: EventBean) {
        if (bean != null && bean.type == EventBusUtils.TYPE_FACE_AND_SIGN_FINISH) {
            var parms = bean.parms as HashMap<String, String?>
            requestServerConfirm(parms)
        }
    }

    private fun requestServerConfirm(signParms: HashMap<String, String?>) {
        var parms = HashMap<String, String?>()
        if (signParms != null) {
            parms.putAll(signParms)
        }
        parms.put("id", commonJumpBean?.bizId)
        parms.put("reason", feedbackView.getInPutText())
        parms.put("result", status)
        parms.put("biz_code", commonJumpBean?.bizCode)
        parms.put("biz_id", commonJumpBean?.bizId)
        EasyHttp.post(this)
            .api(PlatformServiceFeeSignApi())
            .body(JsonBody(parms))
            .request(object : HttpCallback<HttpData<Any?>?>(this) {
                override fun onSucceed(data: HttpData<Any?>?) {
                    showToast(getString(R.string.string_sign_success_tips))
                    finish()
                }
            })
    }

}